layui.use('laydate', function () {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#test-n1',
        position: 'static'
    });
});
function addHlType(){
    $(".tts1").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
}
function cancelData() {
    //更新数据的框关闭
    $(".tts").css({display: 'none'});
    //蒙版消失
    $(".mask").css({display: 'none'});
};
$(function () {
    $(".btn2").click(function () {
        var typename = $(".soctype").val();
        $.post("../soc/addSocType",{"typename":typename},function (data){
            if (data.code == 200 && data.data > 0){
                alert("添加成功")
            }
        });
        cancelData();
        window.location.reload();
    });
    $(".btn3").click(function () {
        var nname = $(".nname").val();
        var ntel = $(".ntel").val();
        var naddr = $(".naddr").val();
        $.post("../soc/addNurse",{"nname":nname,"ntel":ntel,"naddr":naddr},function (data){
            if (data.code == 200 && data.data > 0){
                alert("添加成功")
            }
        });
        cancelData();
        window.location.reload();
    });
    $(".btn4").click(function () {
        var nid = $(".nid").val();
        var nname = $(".nname").val();
        var ntel = $(".ntel").val();
        var naddr = $(".naddr").val();
        $.post("../soc/updateNurse",{"nid":nid,"nname":nname,"ntel":ntel,"naddr":naddr},function (data){
            if (data.code == 200 && data.data > 0){
                alert("修改成功")
            }
        });
        cancelData();
        window.location.reload();
    });
});
var pageNum = 1;
var size = 2;

function nurPatientRel(obj){
    var oid = $(obj).parent().siblings().eq(1).children().val();
    var rstart = $(obj).parent().siblings().eq(2).children().val();
    var rend = $(obj).parent().siblings().eq(3).children().val();
    var nid = $(obj).parent().siblings().eq(4).children().val();
    if (tid == 0 || oid == '' || nid == ''){
        alert("老人、护士、护理类型是必选的！！！");
        return;
    }
    $.post("../soc/nurPatientRel",{tid:tid,oid:oid,rstart:rstart,rend:rend,nid:nid},rs => {
        window.location.reload();
    });
}
var tid = 0;
function showType(obj){
    $(obj).addClass('selected').siblings().removeClass('selected').end();
    $('#ips').val($(obj).text());
    tid = $(obj).attr("data-tno");
}
var uls = new Vue({
    el:"#contain",
    data:{
        soctypes:{},
        rels:{},
        relTotal:0,
        nurses:{},
        nurseTotal:0
    },
    mounted:function (){
        //查询所有的护理类型
        this.findSocType();
        //查询所有的老人
        this.findAllOldman();
        //查询所有的护士
        this.findAllNurse();
        //查询所有的老人和护士的绑定关系
        this.findAllRel();
        //按页码查找护士
        this.findNurseByPage();
    },
    methods:{
        findSocType:function (){
            axios.post("../soc/findSocType").then( rs => {
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        this.soctypes = rs.data.data;
                    }
                }
            })
        },
        findAllOldman:function (){
            axios.post("../oldman/findAllOldman").then(rs =>{
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        var str = "<option></option>";
                        $.each(rs.data.data,function(index,item){
                            str += "<option value='"+item.oid+"'>"+item.oname+"</option>";
                        });
                        $(".oldman").html(str);
                    }
                }
            });
        },
        findAllNurse:function(){
            axios.post("../soc/findAllNurse").then(rs =>{
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        var str = "<option></option>";
                        //下拉框中选择护士
                        $.each(rs.data.data,function(index,item){
                            str += "<option value='"+item.nid+"'>"+item.nname+"</option>";
                        });
                        $(".nurse").html(str);
                    }
                }
            });
        },
        findNurseByPage:function (){
            var startNum = (pageNum -1)*size;
            axios.post("../soc/findNurseByPage",qs.stringify({"startNum":startNum,"size":size})).then(rs => {
                if (rs.status == 200) {
                    if (rs.data.code == 200) {
                        this.nurses = rs.data.data;
                        this.findNurseTotal();
                    }
                }
            });
        },
        findAllRel:function(){
            var startNum = (pageNum - 1) * size;
            axios.post("../soc/findAllRel",qs.stringify({"startNum":startNum,"size":size})).then( rs=>{
               if (rs.status == 200){
                   if (rs.data.code == 200){
                        this.rels = rs.data.data;
                        this.findRelTotal();
                    }
               }
            });
        },
        findRelTotal:function(){
            //查询所有的护理记录条数
            axios.get("../soc/findRelTotal").then(rs=>{
               if (rs.status == 200){
                   if (rs.data.code == 200){
                       this.relTotal = rs.data.data;
                       //生成页码
                       $("#pagination1").showPage((Math.ceil(this.relTotal / size) || 1),1);
                   }
               }
            });
        },
        findNurseTotal:function(){
            //查询所有的护士人数
            axios.get("../soc/findNurseTotal").then(rs=>{
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        this.nurseTotal = rs.data.data;
                        //生成页码
                        $("#pagination2").showPage((Math.ceil(this.nurseTotal / size) || 1),2);
                    }
                }
            });
        }
    }
});
//添加护士
function addNurse(){
    $(".tts2").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
    $(".btn3").css({display:'block'})
    $(".btn4").css({display:'none'})
}
//修改护士
function updateNurse(obj){
    $(".tts2").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
    $(".btn3").css({display:'none'});
    $(".btn4").css({display:'block'})
    var nid = $(obj).parent().siblings().eq(0).text();
    var nname = $(obj).parent().siblings().eq(1).text();
    var ntel = $(obj).parent().siblings().eq(2).text();
    var naddr = $(obj).parent().siblings().eq(3).text();
    $(".nid").val(nid);
    $(".nname").val(nname);
    $(".ntel").val(ntel);
    $(".naddr").val(naddr);
}
function delNurse(obj){
    var nid = $(obj).parent().siblings().eq(0).text();
    $.post("../soc/delNurse",{"nid":nid},res => {
        window.location.reload();
    });
}
//点击页码时需要展示的数据
function showToPage(obj,type){
    //获取页码
    var page = $(obj).text();
    var startNum = (page - 1) * size;
    if (type == 1){
        //说明是点击的是护理记录那里的页码
        $.post("../soc/findAllRel",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                uls.$data.rels = res.data;
            }
        });
    } else {
        $.post("../soc/findNurseByPage",{"startNum":page,"size":size},res => {
            if (res.code == 200){
                uls.$data.nurses = res.data;
            }
        });
    }
}
//动态生成页码的方法
$.fn.showPage = function(count,type) {
    var str = '<li><a href="javascript:void(0)" onclick="showToPage(this,'+type+')">1</a></li>';
    for (var i = 2; i <= count; i ++) {
        str += '<li><a href="javascript:void(0)" onclick="showToPage(this,'+type+')">' + i + '</a></li>';
    }
    var currentObj = $(this);
    currentObj.html("").append($(str));
}